package com.atguigu.yygh.cmn.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/8/23 21:24
 */
public class EasyExcelWriteDemo {

    public static void main(String[] args) {
        // 测试方式二：可以写入多个sheet中，但是需要手动关闭流对象
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student(1, "mgx", 19, true));
        studentList.add(new Student(2, "mgx123", 20, true));
        studentList.add(new Student(3, "mgx1234", 21, true));
        studentList.add(new Student(4, "mgx1235", 22, true));

        List<Student> students = new ArrayList<>();
        students.add(new Student(5, "mgx6666", 21, true));
        students.add(new Student(6, "mgx8888", 22, true));
        ExcelWriter excelWriter = EasyExcel.write("D:\\java_projects\\syt_yygh\\student.xlsx", Student.class).build();
        // 创建sheetBuilder对象
        WriteSheet sheet1 = EasyExcel.writerSheet(0, "学生列表1").build();
        WriteSheet sheet2 = EasyExcel.writerSheet(1, "学生列表2").build();
        // 写入writer中
        excelWriter.write(studentList, sheet1);
        excelWriter.write(students, sheet2);
        // 关闭流对象
        excelWriter.finish();
    }


    /*public static void main(String[] args) {
        // 利用工具类写表格操作
        // 给定数据
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student(1, "mgx", 19, true));
        studentList.add(new Student(2, "mgx123", 20, true));
        studentList.add(new Student(3, "mgx1234", 21, true));
        studentList.add(new Student(4, "mgx1235", 22, true));
        // 方式一： 自动关闭流文件，只能写入一个sheet
        EasyExcel.write("D:\\java_projects\\syt_yygh\\hello.xlsx", Student.class)
                .sheet("学生列表1")
                .doWrite(studentList);
    }*/
}
